<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备类型" prop="devType">
        <el-select v-model="queryParams.devType" placeholder="请选择">
          <el-option
            v-for="item in dict.type.sys_product_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出厂时间" prop="outFactoryTime">
        <el-date-picker
          v-model="queryParams.outFactoryTime"
          type="monthrange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="app版本" prop="devAppVer">
        <el-select v-model="queryParams.devAppVer" clearable filterable placeholder="请选择设备app版本">
            <el-option
             v-for="item in allVersion.filter(f=>f.productId == '0')"
              :key="item.id"
              :label="item.versionId"
              :value="item.id"
            ></el-option>
          </el-select>
        <!-- <el-input
          v-model="queryParams.devAppVer"
          placeholder="请输入app版本"
          clearable
        /> -->
      </el-form-item>
      <el-form-item label="arm版本" prop="devArmVer">
        <el-select v-model="queryParams.devArmVer" clearable filterable placeholder="请选择设备arm版本">
            <el-option
              v-for="item in allVersion.filter(f=>f.productId == '1')"
              :key="item.id"
              :label="item.versionId"
              :value="item.id"
            ></el-option>
          </el-select>
        <!-- <el-input
          v-model="queryParams.devArmVer"
          placeholder="请输入arm版本"
          clearable
        /> -->
      </el-form-item>
      <el-form-item label="厂家名称" prop="devFactoryId">
        <el-select
          v-model="queryParams.devFactoryId"
          clearable
          filterable
          placeholder="请选择厂家"
        >
          <el-option
            v-for="item in dict.type.sys_product_factory"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:deviceVersion:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:deviceVersion:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:deviceVersion:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:deviceVersion:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceVersionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备mac码" align="center" prop="devMac" >
          <template slot-scope="scope">
          <span v-if="(scope.row.devArmVerName || devAppVerName)">{{scope.row.devMac}}</span>
          <span style="color: red;" v-else>{{scope.row.devMac}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="devType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_product_type"
            :value="scope.row.devType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="出厂日期"
        align="center"
        prop="outFactoryTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outFactoryTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="app版本" align="center" prop="devAppVerName">
        <!-- <template slot-scope="scope">
          <dict-tag
            :options="dict.type.official_article_tab"
            :value="scope.row.devAppVer"
          />
        </template> -->
      </el-table-column>
      <el-table-column label="arm版本" align="center" prop="devArmVerName">
        <!-- <template slot-scope="scope">
          <dict-tag
            :options="dict.type.official_article_tab"
            :value="scope.row.devArmVer"
          />
        </template> -->
      </el-table-column>
      <el-table-column label="厂家名称" align="center" prop="devFactoryId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_product_factory"
            :value="scope.row.devFactoryId"
          />
        </template>
      </el-table-column>
       <el-table-column label="标定文件" align="center" prop="devCameraPath">
        <template slot-scope="scope">
          {{scope.row.devCameraPath?'已上传':'未上传'}}
        </template>
      </el-table-column>
      <el-table-column  label="备注" prop="devInfo">
      </el-table-column >
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:deviceVersion:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:deviceVersion:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备版本对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备类型" prop="devType">
          <el-select v-model="form.devType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.sys_product_type"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出厂日期" prop="outFactoryTime">
          <el-date-picker
            clearable
            v-model="form.outFactoryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择设备出厂日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="app版本" prop="devAppVer">
          <el-select v-model="form.devAppVer" filterable placeholder="请选择设备app版本">
            <el-option
             v-for="item in allVersion.filter(f=>f.productId == '0')"
              :key="item.id"
              :label="item.versionId"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="arm版本" prop="devArmVer">
          <el-select v-model="form.devArmVer" filterable placeholder="请选择设备arm版本">
            <el-option
              v-for="item in allVersion.filter(f=>f.productId == '1')"
              :key="item.id"
              :label="item.versionId"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="厂家名称" prop="devFactoryId">
          <el-select v-model="form.devFactoryId" placeholder="请选择厂家">
            <el-option
              v-for="dict in dict.type.sys_product_factory"
              :key="dict.value"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="devInfo">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.devInfo">
          </el-input>
        </el-form-item>
         <el-form-item label="文件" prop="multipartFile">
          <el-upload
            class="upload-demo"
            ref="upload"
            action=""
            :file-list="form.files"
            :limit="1"
            :auto-upload="false"
            accept=".yml"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选择文件</el-button
            >
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDevDetailInfoByCondition,
  delDevInfo,
  modifyDevInfo,uploadEquipmentCameraFile,
} from "@/api/device/deviceVersion";
import { getAllVersion } from "@/api/device/versionManage";

export default {
  name: "DeviceVersion",
  dicts: ["official_article_tab", "sys_product_type", "sys_product_factory"],
  data() {
    return {
      //出厂日期快捷选项
      pickerOptions: {
        shortcuts: [
          {
            text: "本月",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            },
          },
          {
            text: "今年至今",
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备版本表格数据
      deviceVersionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devMac: null,
        devType: null,
        outFactoryTime: null,
        devAppVer: null,
        devArmVer: null,
        devFactoryId: null,
        devInfo:null,
        beginTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        devType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" },
        ],
        outFactoryTime: [
          { required: true, message: "出厂日期不能为空", trigger: "blur" },
        ],
        devAppVer: [
          { required: true, message: "设备app版本不能为空", trigger: "blur" },
        ],
        devArmVer: [
          { required: true, message: "设备arm版本不能为空", trigger: "blur" },
        ],
        devFactoryId: [
          { required: true, message: "厂家不能为空", trigger: "blur" },
        ],
      },
      allVersion:[]
    };
  },
  created() {
    this.getList();
    this.getAllVersion();
  },
  methods: {
    getAllVersion(){
      getAllVersion().then(res=>{
        this.allVersion = res.data
      })
    },
    /** 查询设备版本列表 */
    getList() {
      if (this.queryParams.outFactoryTime) {
        this.queryParams.beginTime = this.queryParams.outFactoryTime[0];
        this.queryParams.endTime = this.queryParams.outFactoryTime[1];
      }
      this.loading = true;
      getDevDetailInfoByCondition(this.queryParams).then((response) => {
        this.deviceVersionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        devMac: null,
        devType: null,
        outFactoryTime: null,
        devAppVer: null,
        devArmVer: null,
        devFactoryId: null,
        devInfo:null,
        files:[],
        multipartFile:null,
        devCameraPath:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.devMac);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备版本";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const devMac = row.devMac || this.ids
      getDevDetailInfoByCondition({ devMac: row.devMac }).then((response) => {
        this.form = response.rows[0];
        this.open = true;
        this.title = "修改设备版本";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.devMac != null) {
            this.form.multipartFile = this.$refs.upload.uploadFiles[0]? this.$refs.upload.uploadFiles[0].raw: null;
            if(this.form.multipartFile){
              let formData = new FormData();
              formData.append("multipartFile", this.form.multipartFile);
              uploadEquipmentCameraFile(formData).then((res) => {
                if(res.code == 200){
                  this.form.devCameraPath = res.msg
                  modifyDevInfo(this.form).then((response) => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                  });
                }
            });
            }else{
              modifyDevInfo(this.form).then((response) => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                  });
            }




            // modifyDevInfo(this.form).then((response) => {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.getList();
            // });
          } else {
            // addDeviceVersion(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const devMacs = row.devMac || this.ids;
      // this.$modal
      //   .confirm('是否确认删除设备版本编号为"' + devMacs + '"的数据项？')
      //   .then(function () {
      //     return delDevInfo(devMacs);
      //   })
      //   .then(() => {
      //     this.getList();
      //     this.$modal.msgSuccess("删除成功");
      //   })
      //   .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('device/deviceVersion/export', {
      //   ...this.queryParams
      // }, `deviceVersion_${new Date().getTime()}.xlsx`)
    },
  },
};
</script>
